package com.bjwl.web.contrlller.branchstore;

import com.bjwl.pojo.*;
import com.bjwl.pojo.dto.SettleBrDto;
import com.bjwl.service.*;
import com.bjwl.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author: czc
 * @Date:Create：in 2020/5/11 17:56
 * @Description:
 * @Modified By：
 * @Version: 1.0
 */
@Controller
@RequestMapping("/Set")
public class SettlementControllerabc {

    @Autowired
    private SettlementService settlementService;
    @Autowired
    private FindBaseService findBaseService;
    @Autowired
    private CardService cardService;
    @Autowired
    private CardStripService cardStripService;
    @Autowired
    private DailyService dailyService;
    @Autowired
    private AttendService attendService;
    @Autowired
    private StaffService staffService;

    private Integer i=0;
    //跳转结算列表页面
    @RequestMapping("/jumpSettleList")
    public String jumpSettleList(HttpSession httpSession,Integer bsid ){
        List<Settlement> settlementList  =  settlementService.findAllSettlementByBsid(bsid);
        httpSession.setAttribute("settlementList",settlementList);
        return "branch_oa/settle-list";
    }



    //跳转结算页面
    @RequestMapping("/jumpUpdateSettle")
    public String jumpUpdateSettle(HttpSession httpSession,Integer id ){
        Bsadmin bsadmin = (Bsadmin)httpSession.getAttribute("bsadmin1");
        List<ServiceClass> serviceClassupSe = findBaseService.findAllServiceClass(bsadmin.getBsid());
        Settlement settlements = settlementService.jumpUpdateSettle(id);
        httpSession.setAttribute("settlements",settlements);
        httpSession.setAttribute("serviceClassupSe",serviceClassupSe);

        return "branch_oa/settle-edit";
    }

    //结算页面点击结算button后输入传过来进行验证结算状态，无误后数据传入数据库，有误返回false进行弹框提醒客户
    @RequestMapping(value = "/updateSettlement",method = RequestMethod.POST)
    public @ResponseBody Boolean updateSettlement(Settlement settlement,HttpSession httpSession){
        //根据id查询数据库中的该项数据，主要是获取结算状态
        Settlement settlement1 = settlementService.jumpUpdateSettle(settlement.getId());
        //根据cardid查询数据库中的该项数据，主要是获取结算状态
       /* Settlement settlement2 = settlementService.findSettlementByCardid(settlement.getMembershipcard());*/
        //首先查看数据库中的状态 ，如果是数据库状态为未结算，那么符合一切流程，前台数据修改数据库
            if(settlement.getSettlestate().equals("已结算")) {

                if(settlement1.getCardid()!=null ){
                    //根据家政卡号去查家政卡
                    Card card = cardService.SfindCardByCardId(settlement.getMembershipcard());
                   /* Card card = cardService.SfindCardByCardId(settlement1.getCardid());*/

                    if(card.getCardtype()==0){
                        if(card.getNworkinghours() >= settlement.getHourseormoney()){
                            settlementService.updateSettlement(settlement);
                            Float nworkinghours =  card.getNworkinghours()-settlement.getHourseormoney();
                            card.setNworkinghours(nworkinghours);
                            cardService.rechargeHoursCard(card);

                            //添加日志家政卡充值工时
                            Daily daily = new Daily();
                            daily.setTime(new Date());
                            daily.setContent("卡号:"+card.getCardid()+"--派单结算工时："+settlement.getHourseormoney());
                            Bsadmin bsadmin = (Bsadmin) httpSession.getAttribute("bsadmin1");
                            daily.setBsid(bsadmin.getBsid());
                            dailyService.addDaily(daily);


                            Card card1 = cardService.SfindCardByCardId(card.getCardid());

                            CardStrip cardStrip = new CardStrip();
                            cardStrip.setSerialnumber(settlement.getOrdersid());
                            cardStrip.setName(card.getName());
                            cardStrip.setCardid(card.getCardid());
                            cardStrip.setYworkinghours(card1.getYworkinghours());
                            cardStrip.setNworkinghours(nworkinghours);
                            cardStrip.setPeriod(settlement.getAppointmenttime());
                            cardStrip.setTel(card.getTel());
                            cardStrip.setDetailedaddress(card.getDetailedaddress());
                            cardStrip.setBsid(card1.getBsid());
                            cardStrip.setCardtype(card1.getCardtype());
                            cardStripService.insertCardStrip(cardStrip);

                            if(settlement.getSnameone() != null &&  settlement.getSnameone() != ""){
                                Attend attend = new Attend();
                                attend.setSname(settlement.getSnameone());
                                attend.setShourse(settlement.getShourseone());
                                attend.setPropaganda(settlement.getPropagandaone());
                                attend.setVacantroom(settlement.getVacantroomone());
                                attend.setCommission(settlement.getCommissionone());
                                attend.setBonus(settlement.getBonusone());
                                attend.setDeduction(settlement.getDeductionone());
                                attend.setSid(settlement.getBsid());
                                attend.setServicetime(settlement.getServicetime());
                                Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
                                attend.setBsid(bsadmin1.getBsid());
                                attendService.addAttend(attend);
                            }
                            if(settlement.getSnametwo() != null  &&  settlement.getSnametwo() != ""){
                                Attend attend = new Attend();
                                attend.setSname(settlement.getSnametwo());
                                attend.setShourse(settlement.getShoursetwo());
                                attend.setPropaganda(settlement.getPropagandatwo());
                                attend.setVacantroom(settlement.getVacantroomtwo());
                                attend.setCommission(settlement.getCommissiontwo());
                                attend.setBonus(settlement.getBonustwo());
                                attend.setDeduction(settlement.getDeductiontwo());
                                attend.setSid(settlement.getBsid());
                                attend.setServicetime(settlement.getServicetime());
                                Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
                                attend.setBsid(bsadmin1.getBsid());
                                attendService.addAttend(attend);
                            }

                            if(settlement.getSnamethree() != null  &&  settlement.getSnamethree() != ""){
                                Attend attend = new Attend();
                                attend.setSname(settlement.getSnamethree());
                                attend.setShourse(settlement.getShoursethree());
                                attend.setPropaganda(settlement.getPropagandathree());
                                attend.setVacantroom(settlement.getVacantroomthree());
                                attend.setCommission(settlement.getCommissionthree());
                                attend.setBonus(settlement.getBonusthree());
                                attend.setDeduction(settlement.getDeductionthree());
                                attend.setSid(settlement.getBsid());
                                attend.setServicetime(settlement.getServicetime());
                                Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
                                attend.setBsid(bsadmin1.getBsid());
                                attendService.addAttend(attend);
                            }
                            if(settlement.getSnamefour() != null &&  settlement.getSnamefour() != "" ){
                                Attend attend = new Attend();
                                attend.setSname(settlement.getSnamefour());
                                attend.setShourse(settlement.getShoursefour());
                                attend.setPropaganda(settlement.getPropagandafour());
                                attend.setVacantroom(settlement.getVacantroomfour());
                                attend.setCommission(settlement.getCommissionfour());
                                attend.setBonus(settlement.getBonusfour());
                                attend.setDeduction(settlement.getDeductionfour());
                                attend.setSid(settlement.getBsid());
                                attend.setServicetime(settlement.getServicetime());
                                Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
                                attend.setBsid(bsadmin1.getBsid());
                                attendService.addAttend(attend);
                            }
                            if(settlement.getSnamefive() != null  &&  settlement.getSnamefive() != ""){
                                Attend attend = new Attend();
                                attend.setSname(settlement.getSnamefive());
                                attend.setShourse(settlement.getShoursefive());
                                attend.setPropaganda(settlement.getPropagandafive());
                                attend.setVacantroom(settlement.getVacantroomfive());
                                attend.setCommission(settlement.getCommissionfive());
                                attend.setBonus(settlement.getBonusfive());
                                attend.setDeduction(settlement.getDeductionfive());
                                attend.setSid(settlement.getBsid());
                                attend.setServicetime(settlement.getServicetime());
                                Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
                                attend.setBsid(bsadmin1.getBsid());
                                attendService.addAttend(attend);
                            }


                        }else {
                            return false;
                        }

                    }else {
                        if(card.getNbalance() >settlement.getHourseormoney()) {
                            settlementService.updateSettlement(settlement);
                            Float nbalance = card.getNbalance() - settlement.getHourseormoney();
                            card.setNbalance(nbalance);
                            cardService.rechargeMoneyCard(card);

                            //添加日志家政卡充值工时
                            Daily daily = new Daily();
                            daily.setTime(new Date());
                            daily.setContent("卡号:"+card.getCardid()+"--派单结算现金："+settlement.getHourseormoney());
                            Bsadmin bsadmin = (Bsadmin) httpSession.getAttribute("bsadmin1");
                            daily.setBsid(bsadmin.getBsid());
                            dailyService.addDaily(daily);


                            Card card1 = cardService.SfindCardByCardId(card.getCardid());


                            CardStrip cardStrip = new CardStrip();
                            cardStrip.setSerialnumber(settlement.getOrdersid());
                            cardStrip.setName(card.getName());
                            cardStrip.setCardid(card.getCardid());
                            cardStrip.setYbalance(card1.getYbalance());
                            cardStrip.setNbalance(nbalance);
                            cardStrip.setPeriod(card.getPeriod());
                            cardStrip.setTel(card.getTel());
                            cardStrip.setDetailedaddress(card.getDetailedaddress());
                            cardStrip.setBsid(card1.getBsid());
                            cardStrip.setCardtype(card1.getCardtype());
                            cardStripService.insertCardStrip(cardStrip);

                            if(settlement.getSnameone() != null &&  settlement.getSnameone() != ""){
                                Attend attend = new Attend();
                                attend.setSname(settlement.getSnameone());
                                attend.setShourse(settlement.getShourseone());
                                attend.setPropaganda(settlement.getPropagandaone());
                                attend.setVacantroom(settlement.getVacantroomone());
                                attend.setCommission(settlement.getCommissionone());
                                attend.setBonus(settlement.getBonusone());
                                attend.setDeduction(settlement.getDeductionone());
                                attend.setSid(settlement.getBsid());
                                attend.setServicetime(settlement.getServicetime());
                                Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
                                attend.setBsid(bsadmin1.getBsid());
                                attendService.addAttend(attend);
                            }
                            if(settlement.getSnametwo() != null  &&  settlement.getSnametwo() != ""){
                                Attend attend = new Attend();
                                attend.setSname(settlement.getSnametwo());
                                attend.setShourse(settlement.getShoursetwo());
                                attend.setPropaganda(settlement.getPropagandatwo());
                                attend.setVacantroom(settlement.getVacantroomtwo());
                                attend.setCommission(settlement.getCommissiontwo());
                                attend.setBonus(settlement.getBonustwo());
                                attend.setDeduction(settlement.getDeductiontwo());
                                attend.setSid(settlement.getBsid());
                                attend.setServicetime(settlement.getServicetime());
                                Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
                                attend.setBsid(bsadmin1.getBsid());
                                attendService.addAttend(attend);
                            }



                            if(settlement.getSnamethree() != null  &&  settlement.getSnamethree() != ""){
                                Attend attend = new Attend();
                                attend.setSname(settlement.getSnamethree());
                                attend.setShourse(settlement.getShoursethree());
                                attend.setPropaganda(settlement.getPropagandathree());
                                attend.setVacantroom(settlement.getVacantroomthree());
                                attend.setCommission(settlement.getCommissionthree());
                                attend.setBonus(settlement.getBonusthree());
                                attend.setDeduction(settlement.getDeductionthree());
                                attend.setSid(settlement.getBsid());
                                attend.setServicetime(settlement.getServicetime());
                                Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
                                attend.setBsid(bsadmin1.getBsid());
                                attendService.addAttend(attend);
                            }
                            if(settlement.getSnamefour() != null &&  settlement.getSnamefour() != "" ){
                                Attend attend = new Attend();
                                attend.setSname(settlement.getSnamefour());
                                attend.setShourse(settlement.getShoursefour());
                                attend.setPropaganda(settlement.getPropagandafour());
                                attend.setVacantroom(settlement.getVacantroomfour());
                                attend.setCommission(settlement.getCommissionfour());
                                attend.setBonus(settlement.getBonusfour());
                                attend.setDeduction(settlement.getDeductionfour());
                                attend.setSid(settlement.getBsid());
                                attend.setServicetime(settlement.getServicetime());
                                Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
                                attend.setBsid(bsadmin1.getBsid());
                                attendService.addAttend(attend);
                            }
                            if(settlement.getSnamefive() != null  &&  settlement.getSnamefive() != ""){
                                Attend attend = new Attend();
                                attend.setSname(settlement.getSnamefive());
                                attend.setShourse(settlement.getShoursefive());
                                attend.setPropaganda(settlement.getPropagandafive());
                                attend.setVacantroom(settlement.getVacantroomfive());
                                attend.setCommission(settlement.getCommissionfive());
                                attend.setBonus(settlement.getBonusfive());
                                attend.setDeduction(settlement.getDeductionfive());
                                attend.setSid(settlement.getBsid());
                                attend.setServicetime(settlement.getServicetime());
                                Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
                                attend.setBsid(bsadmin1.getBsid());
                                attendService.addAttend(attend);

                            }

                        }else {
                            return false;
                        }
                    }
                }else {
                    settlementService.updateSettlement(settlement);
                    return true;
                }
                return true;
            }else if (settlement.getSettlestate().equals("作废")){
                settlementService.updateSettlement(settlement);
                return true;
            } else if (settlement.getSettlestate().equals("未结算")){
                //首先查看数据库中的状态 ，不管是已经结算还是作废，直接返回false
                return false;
            }

            return false;
    }

    @InitBinder
    public void initDateFormate(WebDataBinder dataBinder) {
        dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }
//********************************************************************************************************
    @RequestMapping("/findAllSettlement")
    public String findAllSettlement(HttpSession httpSession, HttpServletRequest request, Integer bsid, Integer pageNum, String wstarttime , String wendtime , String settlestate , Integer membershipcard) {
        if(pageNum == null){
            pageNum =1;
        }
        Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
        bsid = bsadmin1.getBsid();

        PageBean<Settlement> settlementLists = settlementService.findAllSettlement(pageNum,bsid,wstarttime,wendtime ,settlestate ,membershipcard);
        request.setAttribute("settlementLists", settlementLists);
        SettleBrDto settleBrDto = new SettleBrDto(wstarttime,wendtime ,settlestate ,membershipcard);
        request.setAttribute("settleBrDto",settleBrDto);

        return "branch_oa/settle-list";

    }


    //结算选择员工，进行跳转 StaffController




}
